﻿using NUnit.Framework;
using System;

namespace InvoiceInvoker.Logic.Tests
{
	[TestFixture]
	public class InvoiceNumberTests
	{
		[Test, Sequential]
		public void ReadsFromString(
			[Values("11/12/13", "---11/12/13---", "---2011/12/13", "11/12/13---", ":11::12:::2013::::")] string invoiceNumber,
			[Values("N/M/R", "---M/R/N---", "---RR/M/N", "MM/NN/R---", ":N::M:::RR::::")] string format,
			[Values(11, 13, 13, 12, 11)] int expectedNumber,
			[Values(12, 11, 12, 11, 12)] int expectedMonth,
			[Values(2013, 2012, 2011, 2013, 2013)] int expectedYear)
		{
			InvoiceNumber number = new InvoiceNumber(invoiceNumber, format);

			Assert.AreEqual(expectedNumber, number.Number);
			Assert.AreEqual(expectedMonth, number.Month);
			Assert.AreEqual(expectedYear, number.Year);
		}

		[Test, Sequential]
		public void ValidatesCorrectFormat([Values("N/M/R", "NN-MM-RR", "M::R::NN", "NNNNNNNNNNNN/M/R")] string format)
		{
			Assert.AreEqual(true, InvoiceNumber.CheckFormat(format));
		}

		[Test, Sequential]
		public void ValidatesIncorrectFormat([Values("/MM/RR", "1/N/M/R", "N/NN/M/R", "N/MM/MM/R", "NNMM/RR", "N/MR", "RN/M")] string format)
		{
			Assert.AreEqual(false, InvoiceNumber.CheckFormat(format));
		}

		[Test]
		public void ConvertsToString()
		{
			InvoiceNumber number = new InvoiceNumber(1, 2, 2003, "::NNNNN::MM::R::");
			Assert.AreEqual("::00001::02::03::", number.ToString());
		}

		[Test]
		public void GetsNextForCurrentMonth()
		{
			InvoiceNumber number = new InvoiceNumber(10, DateTime.Today.Month, DateTime.Today.Year);
			InvoiceNumber result = number.GetNext();

			Assert.AreEqual(11, result.Number);
			Assert.AreEqual(DateTime.Today.Month, result.Month);
			Assert.AreEqual(DateTime.Today.Year, result.Year);
		}

		[Test]
		public void GetsNextForNewMonth()
		{
			InvoiceNumber number = new InvoiceNumber("0/0/0");
			InvoiceNumber result = number.GetNext();

			Assert.AreEqual(1, result.Number);
			Assert.AreEqual(DateTime.Today.Month, result.Month);
			Assert.AreEqual(DateTime.Today.Year, result.Year);
		}
	}
}
